'use strict'

const Controller = require('egg').Controller
const Joi = require('joi')
const _ = require('lodash')
const uuidv4 = require('uuid/v4')

class TokenController extends Controller {
    // 生成token
    async GetToken() {
        const { app, ctx, service } = this
        const token = app.jwt.sign({ foo: 'bar' }, app.config.jwt.secret, {expiresIn: '1h'})
        ctx.body = token
    }
    // 解析token
    async AuthToken() {
        const { app, ctx, service } = this
        if(!ctx.query || !ctx.query.token) return
        const token = ctx.query.token
        let decoded
        app.jwt.verify(token, app.config.jwt.secret, function(err, dec) {
            console.log(err)
            decoded = dec
        })
        // const decoded = app.jwt.decode(token, {complete: true})
        ctx.body = decoded
    }
}

module.exports = TokenController